package textchorus;

import java.util.Map;

import processing.core.*;
import textchorus.generator.ChordMaker;
import textchorus.sparql.*;
import textchorus.sparql.SparqlQuery.Tag;

public class PSketch extends PApplet {
	/**
	 * 
	 */
	public PFont font;
	public PFont fontB;
	public Voices v;
	public WikiWalker ww;
	public ChordMaker cm;

	private boolean firstRun;
	private boolean fadeFlag = false;

	public static void main(String args[]) {
		PApplet.main(new String[] { "--bgcolor=#111111",
				PSketch.class.getName() });
//		PApplet.main(new String[] { "--present", "--bgcolor=#111111",
//				PSketch.class.getName() });
	}

	public void setup() {
		frameRate(20);
		size(800, 600);
		smooth();
		background(0);

		firstRun = true;
		v = new Voices(this);
		
		font = loadFont("Cambria-48.vlw");
		fontB = loadFont("Bauhaus93-200.vlw");
		
		drawLabel("{ text chorus }");
		

		cm = new ChordMaker(this);
		Map<Tag, String> q = WikiWalker.makeQuery(Tag.S,
				"http://dbpedia.org/resource/Collective_consciousness", null);
		// Map<Tag, String> q = WikiWalker.makeQuery(Tag.O,
		// "http://dbpedia.org/resource/Category:Bombs", null);
		ww = new WikiWalker(this, 50, q);

		// SELECT ?s ?p WHERE {?s ?p
		// <http://dbpedia.org/resource/Category:Action_figures>}
		// SELECT ?p ?o WHERE {<http://dbpedia.org/resource/Robot_Chicken> ?p
		// ?o}
		// SELECT ?s WHERE {?s <http://www.w3.org/2004/02/skos/core#subject>
		// <http://dbpedia.org/resource/Category:Cannibalism>}
		// SELECT ?s WHERE {?s <http://dbpedia.org/property/occupation>
		// <http://dbpedia.org/resource/Poet>}
	}

	public void draw() {
		if(fadeFlag) {
			try {
				fill(0,50);
				rect(0,0,width,height);
			} catch (NullPointerException e) {
				
			}
		}		
	}

	public void keyPressed() {
		if(firstRun) {
			fadeFlag = true;
			firstRun = false;
			ww.doNextHop();
		}
	}
	
	public void drawLabel(String str) {
		textFont(font);
		textAlign(PApplet.CENTER);
		textSize(48);
		fill(255);

		float sw = textWidth(str);
		if (sw > width * .85f) {
			String[] newstr = str.split(" ");
			int i = 0;

			str = "";
			while (i < newstr.length / 2) {
				str = str.concat(newstr[i] + " ");
				i++;
			}
			str = str.concat("\n");
			while (i < newstr.length) {
				str = str.concat(newstr[i] + " ");
				i++;
			}

			System.out.println("####### " + str);
		}

		text(str, width / 2, height / 2);
	}
	
	public void drawPhoneme(String str) {
//		System.out.println("drawing phoneme: " + str);
		
		textFont(fontB);
		textAlign(PApplet.CENTER);
		textSize(600);
		fill(random(255),random(255),random(255),5);

		float sw = textWidth(str);
		if (sw > width * .85f) {
			String[] newstr = str.split(" ");
			int i = 0;

			str = "";
			while (i < newstr.length / 2) {
				str = str.concat(newstr[i] + " ");
				i++;
			}
			str = str.concat("\n");
			while (i < newstr.length) {
				str = str.concat(newstr[i] + " ");
				i++;
			}
		}

		text(str, random(width), random(height));
	}
	
	
	

	public void stop() {
		// always close Minim audio classes
		cm.stop();
		super.stop();
	}

	public boolean isFadeFlag() {
		return fadeFlag;
	}

	public void setFadeFlag(boolean fadeFlag) {
		this.fadeFlag = fadeFlag;
	}
}